// pages/find/find.js
var fonts;
var page=0;
var wxNum;
Page({
  data:{
    questions:[],
    font:"",
    add:true,
    loading:"加载中...",
    NoshowQue:'display',
    showQue:'none',
    
    
  },

  addObject: function (a, b) {
    a = JSON.stringify(a);
    b = JSON.stringify(b);
    a = a.concat('。');
    a = a.replace('}。', "");
    b = b.replace('{', ",");
    a = a.concat(b);
    a = JSON.parse(a);
    return a;
  },
  toquestion:function(){
    wxNum=wx.getStorageSync('login_uuId');
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/checkUserIsBlack',
      data:{
        wxNumber:wxNum
      },
      success:function(res){
        if(res.data=='否'){
          wx.navigateTo({
            url: '../toquestion/toquestion?kind=' + fonts,
            success: function (res) {
              // success
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        }
        else{
          wx.showModal({
            title: '',
            content: '检测您发表了不正当言论，已限制您进行提问。请联系平台人员已解除限制',
          })
        }
      },
    })
  
  },
  onLoad:function(options){
    fonts=options.function;
    this.setData(
      {
        iconImg:options.img
      }
    );
    var font1=fonts.substring(0,4);
    var font2=fonts.substring(5,9);
    wx.setNavigationBarTitle({
      title: fonts,
    })
    // 页面初始化 options为页面跳转所带来的参数
    var that=this;
    that.setData(
      {
        font1:font1,
        font2:font2
      }
    );
    this.questions();
  },
  onReachBottom:function(){
    this.setData(
      {
        add:false
      }
    );
    setTimeout(this.loading,1000);
  },
  onPullDownRefresh:function(){
    page=0;
    this.questions();
  },
  questions: function () {
    var that=this;
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/findQuestionByClass',
      data: {
        id: getApp().globalData.hospitalId,
        page: 0,
        page_size: 10,
        questionClass: fonts
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        console.log(res.data);
        page++;
        wx.stopPullDownRefresh();
        var second;
        var questions = res.data;
        for(var i=0;i<questions.length;i++){
          if(questions[i].isAnonymous=='true')
          questions[i].isAnonymous='匿名用户';

          if(questions[i].questionURLSet.length==0)
          questions[i].imgDisplay='none';
          else
          questions[i].imgDisplay='display';
        }
        for (var i = 0; i < questions.length; i++) {
          var add={'close':"display"};
          questions[i]=that.addObject(questions[i],add);
          if(questions[i].isSolved=='是'){
            questions[i].isSolved="未解答"
          }
          else if (questions[i].isSolved == '已关闭') {
            questions[i].isSolved = "已关闭";
            questions[i].close="none";
          }
          else
            questions[i].isSolved = "未解答";
          
          
          questions[i].f4=questions[i].answerList.length;
          second = res.data[i].answerTime;
          if (second > 60) {
            if (second > 1440) {
              second = parseInt(second / 1440);
              second = second + "天";
            }
            else {
              second = parseInt(second / 60);
              second = second + "小时"
            }
          }
          else {
            second = second + "分钟"
          }
          questions[i].answerTime = second;
        }
        that.setData(
          {
            questions: questions,
            NoshowQue:'none',
            showQue: 'display'
           
          }
        );
        //wx.setStorageSync('feeds1', questions)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
   
  },
  prewImg:function(e){
    var index=e.currentTarget.id;
    var url=this.data.questions[index].questionURLSet;
    var imgs=[];
    for(var i=0;i<url.length;i++){
      imgs.push(url[i].imgURL);
    }
    wx.previewImage({
      urls: imgs,
    })
  },
  loading:function(){
    var that=this;
    page++;
    console.log(page);
    wx.request({
      url:'https://www.liuxuanlong.xyz/fenda/wx/findQuestionByClass',
      data: {
        id: getApp().globalData.hospitalId,
              page: page,
              page_size: 10,
              questionClass:fonts
              },
              method:'GET',
      success:function(res){
        console.log(res.data)
        var oldquestions=that.data.questions;/*wx.getStorageSync('feeds1');*/
        var length=oldquestions.length;
        var second;
        var questions = res.data;
        for (var i = 0; i < questions.length; i++) {

          if (questions[i].questionURLSet.length == 0)
            questions[i].imgDisplay = 'none';
          else
            questions[i].imgDisplay = 'display';
        }
        for (var i = 0; i < res.data.length; i++) {
          if (questions[i].isSolved == '是') {
            questions[i].isSolved = "已解答"
          }
          else if (questions[i].isSolved == '已关闭') {
            questions[i].isSolved = "已关闭";
          }
          else
            questions[i].isSolved = "未解答";
          second = res.data[i].answerTime;
          if (second > 60) {
            if (second > 1440) {
              second = parseInt(second / 1440);
              second = second + "天";
            }
            else {
              second = parseInt(second / 60);
              second = second + "小时"
            }
          }
          else {
            second = second + "分钟"
          }
          questions[i].answerTime = second;

        }
        oldquestions=oldquestions.concat(questions);
        if(length==oldquestions.length)
        that.setData(
          {
            loading:"到底了！",
            
            
            
          }
        );
        //wx.setStorageSync('feeds1', oldquestions);
        else
        that.setData(
          {
            questions:oldquestions,
            loading:'加载中',
            
          }
        );
        setTimeout(that.loadingfail, 1000);
      },
      fail:function(){
        that.setData(
          {
            loading:"没网了",
          },
          
        );
        setTimeout(that.loadingfail, 1000);
      },

    })
    
  },
  loadingfail:function(){
    this.setData(
      {
        add:true
      }
    );
  },
  onReady:function(){
    // 页面渲染完成

  },
  onShow:function(){
    // 页面显示
       var that=this;
 
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})